home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5400 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  87 lines

  1. Path: newsfeed.pitt.edu!dsinc!ub!newserve!rebecca!rpi!not-for-mail
  2. From: tknarr@xmission.com ( Todd Knarr )
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
  4. Subject: Re: Q: Rigorous coding using #if !defined(...) and #include
  5. Date: 3 Feb 1996 09:29:56 -0000
  6. Organization: Chaos Central
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: kanze@gabi-soft.fr
  9. Message-ID: <4ev9uk$qr2@netlab.cs.rpi.edu>
  10. References: <4eo1fs$rr3@netlab.cs.rpi.edu>
  11. Reply-To: tknarr@xmission.com ( Todd Knarr )
  12. NNTP-Posting-Host: netlab.cs.rpi.edu
  13. X-Original-Date: 3 Feb 1996 05:08:47 GMT
  14.  
  15. In <4eo1fs$rr3@netlab.cs.rpi.edu>, David Carr <davidc@marine.simrad.no> writes:
  16. >#if !defined (__MYCLASS_H)
  17. >#define (__MYCLASS_H)
  18. >
  19. >#include "Other.h"
  20. >
  21. >class MyClass
  22. >{
  23. >    ...// Uses something in Other.h
  24. >};
  25. >
  26. >#endif
  27.  
  28. >#if !defined (__OTHER_H)
  29. >#define (__OTHER_H)
  30. >
  31. >#include "MyClass.h"
  32. >
  33. >class Other
  34. >{
  35. >    ...// Uses something in MyClass.h
  36. >};
  37. >
  38. >#endif
  39.  
  40. >What is the preferred method for writing rigorous code and 
  41. >solving this
  42. >problem? 
  43. >Any suggestions as to how to make H files a complete interface?
  44.  
  45. With mutually recursive classes like this, the only solution is to
  46. take all the code from one class that actually uses members of the
  47. other and move it out of the .h file and into it's own source file,
  48. making the class a seperate object module when you're done. The key
  49. is that, while you have to have all the declarations completely
  50. available before any use, you don't have to have the definitions
  51. available. It isn't that VC++ is ignoring #include's in .h files as
  52. that you can't use members of Other in MyClass until Other's header
  53. is completely seen, you can't use members of MyClass in Other until
  54. MyClass's header is completely seen, and one of them has to be seen
  55. before the other. If you move the member function bodies into seperate
  56. source files, then the headers contain no mutual recursion and both
  57. of the headers can be completely seen before any function bodies in
  58. the class source files. The basic structure is:
  59.  
  60. Class.h         : contains class { }; block with no function bodies.
  61. Class.cpp       : contains only function bodies with no class { }; block.
  62.  
  63. With your structure, Other.h would #include MyClass.h, MyClass.h would
  64. #include Other.h, Other.cpp would #include Other.h and MyClass.h, MyClass.cpp
  65. would #include MyClass.h and Other.h, and you would need to compile
  66. Other.cpp and MyClass.cpp and link them into the program.
  67.  
  68. If you want annoying, try dealing with a compile that ignores those
  69. guard #if !defined( ) lines sometime. It is impossible to write code that
  70. does mutual #include's, any attempt to do so results in massive error
  71. spews from perfectly legal code, and the kludges to get around it are
  72. not pretty.
  73.  
  74. --
  75. Todd Knarr : tknarr@xmission.com      |  finger for PGP public key
  76.                                       |  Member, USENET Cabal
  77.  
  78. Seriously, I don't want to die just yet.  I don't care how
  79. good-looking they are, I! don't! want! to! die!"
  80.                                         -- Megazone ( UF1 )
  81.  
  82.  
  83.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  84.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  85.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  86.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  87.